<template>
	<!-- 登录 -->
	<view class="login">
		<!-- 背景图 -->
		<image class="background" src="https://luntan.xiaouyf.cn/uecs-wechat/static/img/qianduan/login_bg.png"
			mode="aspectFill" @load="handleImageLoad"></image>
		<!-- 加载动画 -->
		<view class="loading" v-if="!isImageLoaded">
			<view class="loading-spinner"></view>
		</view>
		<view class="login-main" v-if="isImageLoaded">
			<!-- 退回按钮 -->
			<view class="back-btn" @click="goBack">
				<view class="back-icon">
					<view class="back-arrow"></view>
				</view>
			</view>
			
			<view class="login-content">
				<!-- logo -->
				<view class="logo">
					<image src="https://luntan.xiaouyf.cn/uecs-wechat/static/img/qianduan/logo.png" mode="widthFix">
					</image>
				</view>
				<!-- 提示文字 -->
				<view class="tips">未注册手机号验证通过后将自动注册</view>
				<!-- 登录按钮 -->
				<button v-show="isAgree" class="login-btn" :open-type="'getPhoneNumber'"
					@getphonenumber="handleLogin">本机号码一键登录</button>
				<button v-show="!isAgree" class="login-btn" @click="handleButtonClick">本机号码一键登录</button>
			</view>
			<!-- 用户协议 -->
			<view class="agreement">
				<label class="radio" @click="handleCheckboxClick">
					<view class="checkbox" :class="{ 'checked': isAgree }">
						<view class="checkbox-inner" v-if="isAgree"></view>
					</view>
					<text class="agreement-text">我已阅读并同意</text>
					<text class="agreement-link" @click.stop="openAgreement">《用户协议》</text>
				</label>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				isAgree: false,
				isImageLoaded: false
			}
		},
		onShow() {
			// 检查是否已阅读协议
			const hasReadAgreement = uni.getStorageSync('hasReadAgreement');
			if (hasReadAgreement) {
				this.isAgree = true;
			}
		},
		methods: {
			// 处理图片加载完成
			handleImageLoad() {
				this.isImageLoaded = true;
			},
			// 返回上一页
			goBack() {
				// 获取当前页面栈
				const pages = getCurrentPages();
				console.log('pages', pages.length)
				
				// 如果页面栈长度大于1，说明有上一页，可以返回
				if (pages.length > 0) {
					uni.navigateBack({
						delta: 1,
						success: () => {
							console.log('navigateBack成功');
						},
						fail: (err) => {
							console.log('navigateBack失败:', err);
							// 如果navigateBack失败，尝试返回首页
							uni.redirectTo({
								url: '/pages/index/index'
							});
						}
					});
				} else {
					cons
					// 如果没有上一页，直接跳转到首页
					uni.redirectTo({
						url: '/pages/index/index'
					});
				}
			},
			// 处理按钮点击
			handleButtonClick() {
				if (!this.isAgree) {
					uni.showToast({
						title: '请先同意用户协议',
						icon: 'none'
					})
				}
			},
			// 处理登录
			async handleLogin(e) {
				if (!this.isAgree) {
					uni.showToast({
						title: '请先同意用户协议',
						icon: 'none'
					})
					return;
				}
				let self = this;
				try {
					const code = e.detail.code;
					const res1 = await self.$api.getAccount({
						code: code
					})
					if (res1.succ) {
						// if (res1.data.phoneNumber === '18680844687' || res1.data.phoneNumber === '18723193218' 
						// || res1.data.phoneNumber === '136182466331' || res1.data.phoneNumber === '19123023736' 
						// || res1.data.phoneNumber === '15310374935' || res1.data.phoneNumber === '18166310746' 
						// || res1.data.phoneNumber === '13388986206'){
						// 	uni.showModal({
						// 	    title: '请输入手机号码',
						// 	    editable: true,
						// 	    placeholderText: '请输入11位手机号码',
						// 	    success: async (modalRes) => {
						// 	        if (modalRes.confirm) {
						// 	            const phoneNumber = modalRes.content;
						// 				console.log(phoneNumber)
						// 	            if (!/^1[3-9]\d{9}$/.test(phoneNumber)) {
						// 	                uni.showToast({
						// 	                    title: '请输入正确的手机号码',
						// 	                    icon: 'none'
						// 	                });
						// 	                return;
						// 	            }
						// 	            const requestData = {
						// 	                acountId: phoneNumber
						// 	            }
						// 	            const res = await this.$api.infoLogin(requestData)
						// 	            if (res.succ) {
						// 	                this.$store.dispatch('user/setUserInfo', res.data);
						// 	                this.$store.dispatch('user/setLoginStatus', true);
						// 	                uni.showToast({
						// 	                    title: '注册登录成功',
						// 	                    icon: 'success'
						// 	                });
						// 	                setTimeout(() => {
						// 	                    uni.navigateTo({
						// 	                        url: '/pages/complete-information/complete-information'
						// 	                    });
						// 	                }, 300);
						// 	            } else {
						// 	                uni.showToast({
						// 	                    title: res.message || '操作失败，请重试',
						// 	                    icon: 'none'
						// 	                });
						// 	            }
						// 	        }
						// 	    }
						// 	});
						// 	return;
						// }
						const requestData = {
							acountId: res1.data.phoneNumber
						}
						const res = await this.$api.infoLogin(requestData)
						if (res.succ) {
							this.$store.dispatch('user/setUserInfo', res.data);
							this.$store.dispatch('user/setLoginStatus', true);
							uni.showToast({
								title: '注册登录成功',
								icon: 'success'
							});
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/complete-information/complete-information'
								});
							}, 300);
						} else {
							uni.showToast({
								title: res.message || '操作失败，请重试',
								icon: 'none'
							});
						}
					} else {
						uni.showToast({
							title: res.msg || '登录失败',
							icon: 'none'
						});
					}
				} catch (error) {
					uni.showToast({
						title: '获取手机号失败',
						icon: 'none'
					});
				}
			},
			// 处理复选框点击
			handleCheckboxClick() {
				this.isAgree = !this.isAgree;
				console.log('复选框状态变化:', this.isAgree);
			},
			// 打开用户协议
			openAgreement() {
				// 跳转到用户协议页面
				uni.navigateTo({
					url: '/pages/user-agreement/user-agreement'
				})
			}
		}
	}
</script>

<style lang="less">
	.login {
		position: relative;
		height: 100%;

		.background {
			z-index: 1;
			position: relative;
			width: 100%;
			height: 100%;
		}

		.loading {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			background: linear-gradient(180deg, #150D02 0%, #301E04 100%);
			z-index: 2;

			.loading-spinner {
				width: 60rpx;
				height: 60rpx;
				border: 6rpx solid #fff;
				border-top-color: transparent;
				border-radius: 50%;
				animation: spin 1s linear infinite;
			}
		}

		@keyframes spin {
			to {
				transform: rotate(360deg);
			}
		}

		.login-main {
			z-index: 2;
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			display: flex;
			flex-direction: column;
		}

		.back-btn {
			position: absolute;
			top: 90rpx;
			left: 40rpx;
			z-index: 3;
			width: 80rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: linear-gradient(135deg, rgba(243, 179, 89, 0.9) 0%, rgba(236, 106, 52, 0.9) 100%);
			border-radius: 50%;
			backdrop-filter: blur(10rpx);
			box-shadow: 0 8rpx 32rpx rgba(243, 179, 89, 0.3);
			transition: all 0.3s ease;
			border: 2rpx solid rgba(255, 255, 255, 0.2);

			&:active {
				transform: scale(0.95);
				box-shadow: 0 4rpx 16rpx rgba(243, 179, 89, 0.4);
			}
		}

		.back-icon {
			width: 40rpx;
			height: 40rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;
		}

		.back-arrow {
			width: 20rpx;
			height: 20rpx;
			border-left: 3rpx solid #fff;
			border-top: 3rpx solid #fff;
			transform: rotate(-45deg);
			position: relative;
			
		
		}

		.login-content {
			margin-top: 440rpx;
			padding: 0 40rpx;
			.logo {
				text-align: center;
				margin-bottom: 60rpx;

				image {
					width: 312rpx;
					height: 312rpx;
				}
			}

			.tips {
				text-align: center;
				color: #FCCD8E;
				font-size: 36rpx;
				margin-bottom: 50rpx;
			}

			.login-btn {
				width: 100%;
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;
			background: linear-gradient( 128deg, #F3B359 0%, #EC6A34 100%);
				border-radius: 50rpx;
				color: #fff;
				font-size: 36rpx;
				border: none;
				margin-top: 340rpx;

				&:active {
					opacity: 0.9;
				}
			}
		}

		.agreement {
			margin-top: 78rpx;

			.radio {
				display: flex;
				align-items: center;
				justify-content: center;

				.checkbox {
					width: 32rpx;
					height: 32rpx;
					border: 2rpx solid #FFAF43;
					border-radius: 4rpx;
					position: relative;
					background-color: transparent;

					&.checked {
						background: linear-gradient( 180deg, #F3B359 0%, #EC6A34 100%);
					}

					.checkbox-inner {
						position: absolute;
						left: 50%;
						top: 50%;
						width: 20rpx;
						height: 10rpx;
						transform: translate(-50%, -70%) rotate(-45deg);
						border-left: 2rpx solid #fff;
						border-bottom: 2rpx solid #fff;
					}
				}

				.agreement-text {
					font-size: 28rpx;
					color: #fff;
					opacity: 0.9;
					margin-left: 20rpx;
				}

				.agreement-link {
					font-size: 28rpx;
					color: #FFAF43;
					font-weight: bold;
					text-decoration: underline;
					margin-left: 4rpx;
				}
			}
		}
	}
</style>